package controllers

import (
	"encoding/json"
	"fmt"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
)

type OptometryController struct {
	BaseController
}

func (c *OptometryController) Prepare() {
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/optometry/index"
}

func (c *OptometryController) Index() {
	c.checkAuthor("optometry/index")
	c.display("", "optometry/index_footerjs.html")
}

func (c *OptometryController) DataGrid() {
	c.checkAuthor("optometry/index")
	c.dataGrids()
}

//通用列表数据
func (c *OptometryController) dataGrids() {
	var params models.OptometryQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	params.Deps = c.curUser.ExtendDepartmentList
	data, total := models.OptometryPageList(&params)
	c.jsonDataTable(total, data)
}


func (c *OptometryController) Add() {
	c.checkAuthor("optometry/add")

	mssqlUserInfo := new(models.UserInfo)
	if list,err := mssqlUserInfo.GetOne("1000098"); err != nil {
		fmt.Println(err.Error())
 	} else {
		fmt.Println(list)
	}


	if c.Ctx.Input.IsPost() {
		arch_id, _ := c.GetInt64("arch_id")
		doctor := c.GetString("doctor")
		optometrist := c.GetString("optometrist")
		remarks := c.GetString("remarks")
		chufang := c.GetString("chufang")
		if arch_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		}
		m := new(models.Optometry)
		m.DepartmentId = c.curUser.DepartmentId
		m.CreatedAt = time.Now().Unix()
		m.CreatedBy = c.curUser.RealName
		m.UpdatedBy = c.curUser.RealName
		m.UpdatedAt = time.Now().Unix()
		m.Archives = &models.Archives{Id: arch_id}
		m.Remarks = remarks
		m.Doctor = doctor
		m.Optometrist = optometrist
		m.Chufang = chufang
		m.DocumentNumber = models.CreateOptometryDocumentNumber(&c.curUser)
		if _, err := models.OptometryAdd(m); err != nil {
			c.jsonResult(enums.JRCodeFailed, "验光信息保存失败，请重试", "")
			return
		}
		//微信推送
		//ser := services.WechatTemplateMessageService{Archives: m.Archives, Host: c.Ctx.Input.Site()}
		//go ser.SendOptometryMessage(m)

		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	config := c.getOption()
	c.Data["c"] = config
	c.Data["activeSidebarUrl"] = "/optometry/add"
	c.display("", "optometry/add_footerjs.html")

}

func (c *OptometryController) Edit() {
	c.checkAuthor("optometry/index")

	id, _ := c.GetInt64("opt_id") //验光ID
	c.Data["opt_id"] = id
	box_type := c.GetString("type")
	c.Data["box_type"] = box_type

	if c.Ctx.Input.IsPost() {
		if id == 0 {
			c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		}

		doctor := c.GetString("doctor")
		optometrist := c.GetString("optometrist")
		remarks := c.GetString("remarks")
		chufang := c.GetString("chufang")
		m, err := models.GetOptometryById(id)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		}
		if m.DepartmentId != c.curUser.DepartmentId {
			c.jsonResult(enums.JRCodeFailed, "数据无效", "")
		}
		m.UpdatedBy = c.curUser.RealName
		m.UpdatedAt = time.Now().Unix()
		m.Remarks = remarks
		m.Doctor = doctor
		m.Optometrist = optometrist

		m.Chufang = chufang
		if err := m.OptometryEdit(); err != nil {
			c.jsonResult(enums.JRCodeFailed, "验光信息保存失败，请重试", "")
			return
		}

		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	config := c.getOption()
	c.Data["c"] = config
	c.display("", "optometry/edit_footerjs.html", "shared/layout_dialog.html")
}

//获取配置信息
func (c *OptometryController) getOption() (o map[string]interface{}) {

	o = make(map[string]interface{})

	f := models.OptionsQueryParam{}
	f.Status = "1"
	f.Depid = c.curUser.DepartmentId
	//获取医生
	f.Typeid = "10"
	o["doctor"] = models.OptionsList(&f)

	f.Typeid = "11"
	o["optometrist"] = models.OptionsList(&f)

	return
}

func (c *OptometryController) Delete() {
	c.checkAuthor("optometry/delete")
	id, err := c.GetInt64("id")
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	if err := models.OptometryDelete(id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "信息删除成功", "")
}

//查看验光数据
func (c *OptometryController) View() {
	c.checkAuthor("optometry/index")
	optId, _ := c.GetInt64("opt_id")
	c.Data["opt_id"] = optId
	if c.Ctx.Input.IsPost() {
		optometry, err := models.GetOptometryById(optId)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		if !c.checkDepControl(optometry.DepartmentId) {
			c.jsonResult(enums.JRCodeFailed, "验光处方不存在", "")
		}
		optometry.ChufangData = models.GetOptometryDataList(optId)

		c.jsonResult(enums.JRCodeSucc, "ok", optometry)
	}
	c.display("", "optometry/view_footerjs.html")
}

//查看单条验光数据,JSON返回
func (c *OptometryController) ViewOne() {
	c.checkLogin()

	if c.Ctx.Input.IsPost() {
		arcId, _ := c.GetInt64(":id") //档案ID
		data, err := models.GetOptometryById(arcId)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		data.ChufangData = models.GetOptometryDataList(data.Id)
		c.jsonResult(enums.JRCodeSucc, "获取成功", data)
	}

	c.jsonResult(enums.JRCodeFailed, "获取指定参数出错！", "")
}


//处方浏览
func (c *OptometryController) List() {
	c.checkLogin()
	if c.Ctx.Input.IsPost() {

	}
	c.Data["activeSidebarUrl"] = "/optometry/list"
	c.display("", "optometry/list_footerjs.html")
}